package com.shwread.android.qysw.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ciko.code.bean.UserBean;
import com.shwread.android.qysw10000152.R;
import com.shwread.android.ui.dialog.DatePickerDialog;
import com.shwread.android.ui.dialog.DatePickerListener;
import com.shwread.android.ui.dialog.RegisterSexDialog;
import com.shwread.android.ui.dialog.SexListener;
import com.shwread.http.ActionHelp;
import com.shwread.http.base.TextHttpResponseHandler;
import com.shwread.http.util.JSONUtil;
import com.shwread.httpsdk.util.Base64;

import org.apache.http.Header;
import org.json.JSONException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import logic.dao.UserDao;
import logic.util.Util;


public class RegisterInformationActivity
        extends
        Activity
        implements
        OnClickListener,
        SexListener
{
	private TextView          tvTitle;
	private Button            btnBack;
	private EditText          registerPwd;
	private EditText          registerPwdConfirm;
	private EditText          registerName;
	private TextView          registerSex;
	private EditText          registerNickName;
	
	private TextView          registerBirthday;//register_birthday
	private EditText          registerHometown;//register_hometown
	private EditText          registerLivenow;//register_livenow
	private EditText          registerJob;//register_job
	private EditText          registerInterest;//register_interest
	private EditText          registerDesc;//register_desc
	
	private LinearLayout      llSelectSex;
	private Button            registerFinish;
	
	private String            mAccount;
	private String            password;
	private int               sexNum;
	private String            username;
	private String            nickname;
	private UserBean user = new UserBean();
	private Context           context;
	
	private RegisterSexDialog sexDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_information);
		context = this;
		Intent intent = getIntent();
		mAccount = intent.getStringExtra("account");
		user.setAccount(mAccount);
		findViews();
		setListeners();
	}
	
	private void setListeners() {
		llSelectSex.setOnClickListener(this);
		registerBirthday.setOnClickListener(this);
		registerFinish.setOnClickListener(this);
		btnBack.setOnClickListener(this);
	}
	
	private void findViews() {
		tvTitle = (TextView) findViewById(R.id.common_header_name);
		tvTitle.setText("个人资料");
		btnBack = (Button) findViewById(R.id.common_header_back);
		btnBack.setVisibility(View.VISIBLE);
		registerPwd = (EditText) findViewById(R.id.register_psd);
		registerPwdConfirm = (EditText) findViewById(R.id.register_psd_confirm);
		registerName = (EditText) findViewById(R.id.register_name);
		registerSex = (TextView) findViewById(R.id.register_sex);
		registerNickName = (EditText) findViewById(R.id.register_nickname);
		
		registerBirthday = (TextView) findViewById(R.id.register_birthday);
		registerHometown = (EditText) findViewById(R.id.register_hometown);
		registerLivenow = (EditText) findViewById(R.id.register_livenow);
		registerJob = (EditText) findViewById(R.id.register_job);
		registerInterest = (EditText) findViewById(R.id.register_interest);
		registerDesc = (EditText) findViewById(R.id.register_desc);
		
		llSelectSex = (LinearLayout) findViewById(R.id.ll_register_select_sex);
		registerFinish = (Button) findViewById(R.id.register_finish);
		//		registerPwd.setOnFocusChangeListener(new OnFocusChangeListener()
		//		{
		//			
		//			@Override
		//			public void onFocusChange(View v, boolean hasFocus) {
		//				if (!hasFocus)
		//				{
		//					if (registerPwd.getText().toString().trim().length() < 6)
		//					{
		//						Toast.makeText(RegisterInformationActivity.this, "密码是6-16位哦", Toast.LENGTH_SHORT).show();
		//					}
		//					else if (registerPwd.getText().toString().trim().length() > 16)
		//					{
		//						Toast.makeText(RegisterInformationActivity.this, "密码是6-16位哦", Toast.LENGTH_SHORT).show();
		//					}
		//				}
		//			}
		//		});
		//		registerPwdConfirm.setOnFocusChangeListener(new OnFocusChangeListener()
		//		{
		//			
		//			@Override
		//			public void onFocusChange(View v, boolean hasFocus) {
		//				// TODO Auto-generated method stub
		//				if (!hasFocus)
		//				{
		//					if (!registerPwdConfirm.getText().toString().trim().equals(registerPwd.getText().toString().trim()))
		//					{
		//						Toast.makeText(RegisterInformationActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
		//					}
		//				}
		//			}
		//			
		//		});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.register_birthday:
				new DatePickerDialog(this, "", new MyDatePickerListener()).show();
				break;
			case R.id.ll_register_select_sex:
				sexDialog = new RegisterSexDialog(context, -1, this);
				sexDialog.show();
				break;
			case R.id.register_finish:
				register();
				break;
			case R.id.common_header_back:
				finish();
				break;
			default:
				break;
		}
	}
	private void register(){
		password = registerPwd.getText().toString().trim();
		username = registerName.getText().toString().trim();
		nickname = registerNickName.getText().toString().trim();
		user.setPassword(Base64.encode(password, "utf-8"));
		user.setNickName(nickname);
		if (checkMsg())
		{
			String birthdayContent = registerBirthday.getText().toString();
			if(!TextUtils.isEmpty(birthdayContent) && birthdayContent.equals("请选择")){
				birthdayContent = "";
			}
			final String hometownContent = registerHometown.getText().toString();
			final String livenowContent = registerLivenow.getText().toString();
			final String jobContent = registerJob.getText().toString();
			final String interestContent = registerInterest.getText().toString();
			final String descContent = registerDesc.getText().toString();
			ActionHelp.registerAccount(context, mAccount, password, username, sexNum, nickname,
			                           birthdayContent,hometownContent,livenowContent,jobContent,interestContent,descContent,
			                           new RegisterHandler());
		}
	}
	
	private class MyDatePickerListener
        implements
        DatePickerListener
    {
        @Override
        public void onSelect(String value) {
        	registerBirthday.setText(value);
        }
    }
	
	@Override
	public void onSelect(int sex) {
		if (sex == 0)
		{
			sexNum = 0;
			registerSex.setText("男");
			user.setSex(sex + "");
		}
		else if (sex == 1)
		{
			sexNum = 1;
			registerSex.setText("女");
		}
	}
	
	private class RegisterHandler
	        extends
	        TextHttpResponseHandler
	{
		
		@Override
		public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
			if (statusCode > 1000)
			{
				try
				{
					Util.showToast(RegisterInformationActivity.this, JSONUtil.getString(responseString));
				}
				catch (JSONException e)
				{
					e.printStackTrace();
				}
			}
		}
		
		@Override
		public void onSuccess(int statusCode, Header[] headers, String responseString) {
			UserDao.getInstance(context).addUser(user);
			Intent backLoginIntent = new Intent(RegisterInformationActivity.this, NewLoginActivity.class);
			backLoginIntent.putExtra("mark", 1);
			backLoginIntent.putExtra("account", mAccount);
			startActivity(backLoginIntent);
			finish();
		}
		
	}
	
	private boolean checkMsg() {
		String pwd = registerPwd.getText().toString().trim();
		Pattern p = Pattern.compile("[0-9a-zA-Z]*");
		Matcher m = p.matcher(pwd);
		if (registerPwd.getText().toString().trim().equals(""))
		{
			Toast.makeText(RegisterInformationActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
		}
		else if (!m.matches())
		{
			Toast.makeText(RegisterInformationActivity.this, "密码不能加入特殊字符", Toast.LENGTH_SHORT).show();
		}
		else if (registerPwdConfirm.getText().toString().trim().equals(""))
		{
			Toast.makeText(RegisterInformationActivity.this, "请再次输入密码", Toast.LENGTH_SHORT).show();
		}
		else if (!registerPwd.getText().toString().trim().equals(registerPwdConfirm.getText().toString().trim()))
		{
			Toast.makeText(RegisterInformationActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
		}
		else if (TextUtils.isEmpty(registerName.getText().toString().trim()))
		{
			Toast.makeText(RegisterInformationActivity.this, "姓名不能空", Toast.LENGTH_SHORT).show();
		}
		else if (registerName.getText().toString().trim().length() > 20)
		{
			Toast.makeText(RegisterInformationActivity.this, "姓名不能超过20个字", Toast.LENGTH_SHORT).show();
		}
		else if (registerNickName.getText().toString().trim().length() > 6)
		{
			Toast.makeText(RegisterInformationActivity.this, "昵称不能超过6个字", Toast.LENGTH_SHORT).show();
		}
		else if (registerPwd.getText().toString().trim().length() < 6 ||
				registerPwd.getText().toString().trim().length() > 16 )
		{
			Toast.makeText(RegisterInformationActivity.this, "密码是6-16位哦", Toast.LENGTH_SHORT).show();
		}
		else if (TextUtils.isEmpty(registerSex.getText().toString().trim()))
		{
			Toast.makeText(RegisterInformationActivity.this, "性别不能空", Toast.LENGTH_SHORT).show();
		}
		else if (TextUtils.isEmpty(registerNickName.getText().toString().trim()))
		{
			Toast.makeText(RegisterInformationActivity.this, "昵称不能空", Toast.LENGTH_SHORT).show();
		}
		else if (TextUtils.isEmpty(registerHometown.getText().toString().trim()))
		{
			Toast.makeText(RegisterInformationActivity.this, "家乡不能空", Toast.LENGTH_SHORT).show();
		}
		else
		{
			return true;
		}
		return false;
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
}
